home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / Mesa-1.2.1 / src-aux / glaux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-05  |  7.7 KB  |  356 lines

  1. /* aux.c */
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #if !defined(AMIGA) && !defined(__WIN32__)
  8. #include <X11/Xlib.h>
  9. #include <X11/Xutil.h>
  10. #include <X11/keysym.h>
  11. #endif
  12. #include <GL/gl.h>
  13. #include "tk.h"
  14. #include "glaux.h"
  15.  
  16. #if defined(__cplusplus) || defined(c_plusplus)
  17. #define class c_class
  18. #endif
  19.  
  20.  
  21. static struct {
  22.     int keyField;
  23.     void (*KeyFunc)(void);
  24. } keyTable[200];
  25.  
  26. static struct {
  27.     int mouseField;
  28.     void (*MouseFunc)(AUX_EVENTREC *);
  29. } mouseDownTable[20], mouseUpTable[20], mouseLocTable[20];
  30.  
  31. static int keyTableCount = 0;
  32. static int mouseDownTableCount = 0;
  33. static int mouseUpTableCount = 0;
  34. static int mouseLocTableCount = 0;
  35. static GLenum displayModeType = 0;
  36.  
  37.  
  38. #ifdef __WIN32__
  39. #define NCOLORS 17
  40. float auxRGBMap[NCOLORS][3] = {
  41.     {0,0,0},
  42.     {0,0,0},
  43.     {0,0,0},
  44.     {0,0,0},
  45.     {0,0,0},
  46.     {0,0,0},
  47.     {0,0,0},
  48.     {0,0,0},
  49.     {0,0,0},
  50.     {0,0,0},
  51.     {1,0,0},
  52.     {0,1,0},
  53.     {1,1,0},
  54.     {0,0,1},
  55.     {1,0,1},
  56.     {0,1,1},
  57.     {1,1,1}
  58. };
  59. #endif
  60.  
  61.  
  62. static void DefaultHandleReshape(int w, int h)
  63. {
  64.     glViewport(0, 0, w, h);
  65.     glMatrixMode(GL_PROJECTION);
  66.     glLoadIdentity();
  67.     glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0);
  68.     glMatrixMode(GL_MODELVIEW);
  69.     glLoadIdentity();
  70. }
  71.  
  72. static void DefaultHandleExpose(int w, int h)
  73. {
  74. }
  75.  
  76. static GLenum MouseLoc(int x, int y, GLenum button)
  77. {
  78.     AUX_EVENTREC info;
  79.     GLenum flag;
  80.     int i;
  81.  
  82.     flag = GL_FALSE;
  83.     for (i = 0; i < mouseLocTableCount; i++) {
  84.     if ((button & AUX_LEFTBUTTON) == mouseLocTable[i].mouseField) {
  85.         info.event = AUX_MOUSELOC;
  86.         info.data[AUX_MOUSEX] = x;
  87.         info.data[AUX_MOUSEY] = y;
  88.         info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  89.         (*mouseLocTable[i].MouseFunc)(&info);
  90.         flag |= GL_TRUE;
  91.     }
  92.     if ((button & AUX_RIGHTBUTTON) == mouseLocTable[i].mouseField) {
  93.         info.event = AUX_MOUSELOC;
  94.         info.data[AUX_MOUSEX] = x;
  95.         info.data[AUX_MOUSEY] = y;
  96.         info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  97.         (*mouseLocTable[i].MouseFunc)(&info);
  98.         flag |= GL_TRUE;
  99.     }
  100.     if ((button & AUX_MIDDLEBUTTON) == mouseLocTable[i].mouseField) {
  101.         info.event = AUX_MOUSELOC;
  102.         info.data[AUX_MOUSEX] = x;
  103.         info.data[AUX_MOUSEY] = y;
  104.         info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  105.         (*mouseLocTable[i].MouseFunc)(&info);
  106.         flag |= GL_TRUE;
  107.     }
  108.     }
  109.     return flag;
  110. }
  111.  
  112. static GLenum MouseUp(int x, int y, GLenum button)
  113. {
  114.     AUX_EVENTREC info;
  115.     GLenum flag;
  116.     int i;
  117.  
  118.     flag = GL_FALSE;
  119.     for (i = 0; i < mouseUpTableCount; i++) {
  120.     if ((button & AUX_LEFTBUTTON) == mouseUpTable[i].mouseField) {
  121.         info.event = AUX_MOUSEUP;
  122.         info.data[AUX_MOUSEX] = x;
  123.         info.data[AUX_MOUSEY] = y;
  124.         info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  125.         (*mouseUpTable[i].MouseFunc)(&info);
  126.         flag |= GL_TRUE;
  127.     }
  128.     if ((button & AUX_RIGHTBUTTON) == mouseUpTable[i].mouseField) {
  129.         info.event = AUX_MOUSEUP;
  130.         info.data[AUX_MOUSEX] = x;
  131.         info.data[AUX_MOUSEY] = y;
  132.         info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  133.         (*mouseUpTable[i].MouseFunc)(&info);
  134.         flag |= GL_TRUE;
  135.     }
  136.     if ((button & AUX_MIDDLEBUTTON) == mouseUpTable[i].mouseField) {
  137.         info.event = AUX_MOUSEUP;
  138.         info.data[AUX_MOUSEX] = x;
  139.         info.data[AUX_MOUSEY] = y;
  140.         info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  141.         (*mouseUpTable[i].MouseFunc)(&info);
  142.         flag |= GL_TRUE;
  143.     }
  144.     }
  145.     return flag;
  146. }
  147.  
  148. static GLenum MouseDown(int x, int y, GLenum button)
  149. {
  150.     AUX_EVENTREC info;
  151.     GLenum flag;
  152.     int i;
  153.  
  154.     flag = GL_FALSE;
  155.     for (i = 0; i < mouseDownTableCount; i++) {
  156.     if ((button & AUX_LEFTBUTTON) == mouseDownTable[i].mouseField) {
  157.         info.event = AUX_MOUSEDOWN;
  158.         info.data[AUX_MOUSEX] = x;
  159.         info.data[AUX_MOUSEY] = y;
  160.         info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  161.         (*mouseDownTable[i].MouseFunc)(&info);
  162.         flag |= GL_TRUE;
  163.     }
  164.     if ((button & AUX_RIGHTBUTTON) == mouseDownTable[i].mouseField) {
  165.         info.event = AUX_MOUSEDOWN;
  166.         info.data[AUX_MOUSEX] = x;
  167.         info.data[AUX_MOUSEY] = y;
  168.         info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  169.         (*mouseDownTable[i].MouseFunc)(&info);
  170.         flag |= GL_TRUE;
  171.     }
  172.     if ((button & AUX_MIDDLEBUTTON) == mouseDownTable[i].mouseField) {
  173.         info.event = AUX_MOUSEDOWN;
  174.         info.data[AUX_MOUSEX] = x;
  175.         info.data[AUX_MOUSEY] = y;
  176.         info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  177.         (*mouseDownTable[i].MouseFunc)(&info);
  178.         flag |= GL_TRUE;
  179.     }
  180.     }
  181.     return flag;
  182. }
  183.  
  184. static GLenum KeyDown(int key, GLenum status)
  185. {
  186.     GLenum flag;
  187.     int i;
  188.  
  189.     flag = GL_FALSE;
  190.     if (keyTableCount) {
  191.     for (i = 0; i < keyTableCount; i++) {
  192.         if (key == keyTable[i].keyField) {
  193.         (*keyTable[i].KeyFunc)();
  194.         flag |= GL_TRUE;
  195.         }
  196.     }
  197.     }
  198.     return flag;
  199. }
  200.  
  201. void auxExposeFunc(void (*Func)(int, int))
  202. {
  203.     tkExposeFunc(Func);
  204. }
  205.  
  206. void auxReshapeFunc(void (*Func)(int, int))
  207. {
  208.     tkExposeFunc(Func);
  209.     tkReshapeFunc(Func);
  210. }
  211.  
  212. void auxIdleFunc(void (*Func)(void))
  213. {
  214.     tkIdleFunc(Func);
  215. }
  216.  
  217. void auxKeyFunc(int key, void (*Func)(void))
  218. {
  219.     keyTable[keyTableCount].keyField = key;
  220.     keyTable[keyTableCount++].KeyFunc = Func;
  221. }
  222.  
  223. void auxMouseFunc(int mouse, int mode, void (*Func)(AUX_EVENTREC *))
  224. {
  225.     if (mode == AUX_MOUSEDOWN) {
  226.     mouseDownTable[mouseDownTableCount].mouseField = mouse;
  227.     mouseDownTable[mouseDownTableCount++].MouseFunc = Func;
  228.     } else if (mode == AUX_MOUSEUP) {
  229.     mouseUpTable[mouseUpTableCount].mouseField = mouse;
  230.     mouseUpTable[mouseUpTableCount++].MouseFunc = Func;
  231.     } else if (mode == AUX_MOUSELOC) {
  232.     mouseLocTable[mouseLocTableCount].mouseField = mouse;
  233.     mouseLocTable[mouseLocTableCount++].MouseFunc = Func;
  234.     } 
  235. }
  236.  
  237. void auxMainLoop(void (*Func)(void))
  238. {
  239.     tkDisplayFunc(Func);
  240.     tkExec();
  241. }
  242.  
  243. void auxInitPosition(int x, int y, int width, int height)
  244. {
  245.     tkInitPosition(x, y, width, height);
  246. }
  247.  
  248. void auxInitDisplayMode(GLenum type)
  249. {
  250.     displayModeType = type;
  251.     tkInitDisplayMode(type);
  252. }
  253.  
  254. GLenum auxInitWindow(char *title)
  255. {
  256.     int useDoubleAsSingle = 0;
  257.  
  258.     if (tkInitWindow(title) == GL_FALSE) {
  259.     if (AUX_WIND_IS_SINGLE(displayModeType)) {
  260.         tkInitDisplayMode(displayModeType|AUX_DOUBLE);
  261.         if (tkInitWindow(title) == GL_FALSE) {
  262.         return GL_FALSE;
  263.         }
  264.         fprintf(stderr, "Can't initialize a single buffer visual.\n");
  265.         fprintf(stderr, "Will use a double buffer visual instead,");
  266.         fprintf(stderr, "only drawing into the front buffer.\n");
  267.         displayModeType = displayModeType | AUX_DOUBLE;
  268.         useDoubleAsSingle = 1;
  269.     }
  270.     }
  271.     tkReshapeFunc(DefaultHandleReshape);
  272.     tkExposeFunc(DefaultHandleExpose);
  273.     tkMouseUpFunc(MouseUp);
  274.     tkMouseDownFunc(MouseDown);
  275.     tkMouseMoveFunc(MouseLoc);
  276.     tkKeyDownFunc(KeyDown);
  277.     auxKeyFunc(AUX_ESCAPE, auxQuit);
  278.     glClearColor(0.0, 0.0, 0.0, 1.0);
  279.     glClearIndex(0);
  280.     glLoadIdentity();
  281.     if (useDoubleAsSingle) {
  282.         glReadBuffer(GL_FRONT);
  283.     glDrawBuffer(GL_FRONT);
  284.     }
  285.     return GL_TRUE;
  286. }
  287.  
  288. void auxCloseWindow(void)
  289. {
  290.     tkCloseWindow();
  291.     keyTableCount = 0;
  292.     mouseDownTableCount = 0;
  293.     mouseUpTableCount = 0;
  294.     mouseLocTableCount = 0;
  295. }
  296.  
  297. void auxQuit(void)
  298. {
  299.     tkQuit();
  300. }
  301.  
  302. void auxSwapBuffers(void)
  303. {
  304.     tkSwapBuffers();
  305. }
  306.  
  307. #if !defined(AMIGA) && !defined(__WIN32__)
  308. /* for systems with X only... */
  309. Display *auxXDisplay(void)
  310. {
  311.     Display *ptr;
  312.     
  313.     tkGetSystem(TK_X_DISPLAY, (void *)&ptr);
  314.     return ptr;
  315. }
  316.  
  317. Window auxXWindow(void)
  318. {
  319.     Window ptr;
  320.     
  321.     tkGetSystem(TK_X_WINDOW, (void *)&ptr);
  322.     return ptr;
  323. }
  324. #endif
  325.  
  326. void auxSetOneColor(int index, float r, float g, float b)
  327. {
  328.     tkSetOneColor(index, r, g, b);
  329. }
  330.  
  331. void auxSetFogRamp(int density, int startIndex)
  332. {
  333.     tkSetFogRamp(density, startIndex);
  334. }
  335.  
  336. void auxSetGreyRamp(void)
  337. {
  338.     tkSetGreyRamp();
  339. }
  340.  
  341. void auxSetRGBMap(int size, float *rgb)
  342. {
  343.     tkSetRGBMap(size, rgb);
  344. }
  345.  
  346. int auxGetColorMapSize(void)
  347. {
  348.  
  349.     return tkGetColorMapSize();;
  350. }
  351.  
  352. void auxGetMouseLoc(int *x, int *y)
  353. {
  354.     tkGetMouseLoc(x, y);
  355. }
  356.